################################################################################
# Judicial Ideology as Text -- Figure 2: Static Compared to MQ
# Truscott & Romano
# Updated February 2025
################################################################################


################################################################################
# Load Source
################################################################################

source('R/truscott_romano_source.R') # Load Source

################################################################################
# Figure 2: High Static Ideal Points v. MQ
################################################################################

{

  figure_2 <- mq %>%
    dplyr::select(term, justiceName, post_mn, post_025, post_975) %>%
    filter(term >= 2005) %>%
    filter(term <= 2021) %>%
    mutate(justiceName = sub(".*([A-Z])", "\\1", justiceName)) %>%
    group_by(justiceName) %>%
    summarise(mean = mean(post_mn)) %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName)) %>%
    left_join(wordshoals_static$High$summary_output %>%
                dplyr::select(author, theta) %>%
                rename(justiceName = author),
              by = 'justiceName') %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName)) %>%
    rename(mq_mean = mean,
           wordshoal = theta,
           justice = justiceName) %>%
    ggplot(aes(y = forcats::fct_reorder(justice, mq_mean))) +
    geom_segment(aes(x = mq_mean, xend = wordshoal, y = justice, yend = justice), linewidth = 1.25) +
    geom_point(aes(x = mq_mean, colour = "Martin-Quinn", shape = "Martin-Quinn", fill = 'Martin-Quinn'), size = 3) +
    geom_point(aes(x = wordshoal, colour = "Wordshoal", shape = "Wordshoal", fill = 'Wordshoal'), size = 2.5) +
    scale_colour_manual(name = "Measure", values = c("Martin-Quinn" = "black", "Wordshoal" = "black")) +
    scale_shape_manual(name = "Measure", values = c("Martin-Quinn" = 21, "Wordshoal" = 22)) +
    scale_fill_manual(name = 'Measure', values  = c('Martin-Quinn' = 'white', 'Wordshoal' = 'grey')) +
    theme_minimal() +
    theme_minimal() +
    labs(y = ' ',
         x = '\nMartin-Quinn Scale') +
    scale_x_continuous(breaks = seq(-3, 3, 1)) +
    geom_vline(xintercept = 0, linetype = 2, alpha = 1/2) +
    theme(panel.border = element_rect(size = 1, colour = 'black', fill = NA),
          axis.text = element_text(size = 12, colour = 'black'),
          axis.title = element_text(size = 14, colour = 'black'),
          legend.position = 'bottom',
          legend.text = element_text(size = 14, colour = 'black'),
          legend.box.background = element_rect(size = 1, colour = 'black', fill = NA),
          legend.title = element_blank())


  correlation <- mq %>%
    dplyr::select(term, justiceName, post_mn, post_025, post_975) %>%
    filter(term >= 2005) %>%
    filter(term <= 2021) %>%
    mutate(justiceName = sub(".*([A-Z])", "\\1", justiceName)) %>%
    group_by(justiceName) %>%
    summarise(mean = mean(post_mn)) %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName)) %>%
    left_join(wordshoals_static$High$summary_output %>%
                dplyr::select(author, theta) %>%
                rename(justiceName = author),
              by = 'justiceName') %>%
    mutate(justiceName = ifelse(justiceName == 'Connor', "O'Connor", justiceName)) %>%
    rename(mq_mean = mean,
           wordshoal = theta,
           justice = justiceName)

  correlation <- cor(correlation$mq_mean, correlation$wordshoal, method = 'pearson')

  message('Correlation b/w MQ & Static Wordshoal: ', round(correlation, 3))

  ggsave(figure_2,
         file = 'figures/Figure_2.png',
         width = 10,
         height = 8,
         units = 'in',
         bg = 'white')

  ggsave('figures_tiff/Figure_2.tiff',
         plot = figure_2,
         device = 'tiff',
         dpi = 600,
         width = 10,
         height = 10,
         units = 'in',
         bg = 'white')


} # Figure 2: Static Ideal v. Martin-Quinn (Unadjusted Scales)

